/*
 * Photo Import Project
 * 2010-08-14
 * 
 */
import org.eclipse.swt.*;
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.*;

public class StartMain {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		final Display display = new Display ();
		
		final Shell shell = createMainShell(display);
		
		
		while (!shell.isDisposed ()) {
			if (!display.readAndDispatch ()) display.sleep ();
		}
		display.dispose ();
	}

	/*
	 * Create controls on main shell.
	 * Returns created shell 
	 */
	private static Shell createMainShell(Display display) {
		final Shell shell = new Shell(display);
		shell.setText("Photo Import");
		Image img = new Image(display,"file-import-icon 48x48.png");
		shell.setImage(img);
		
		//Create a menubar
		Menu mainMenu = new Menu (shell, SWT.BAR);
		shell.setMenuBar (mainMenu);
		
		//Create File menu
		MenuItem fileItem = new MenuItem (mainMenu, SWT.CASCADE);
		fileItem.setText ("&File");
		Menu submenu = new Menu (shell, SWT.DROP_DOWN);
		fileItem.setMenu (submenu);
		MenuItem item = new MenuItem (submenu, SWT.PUSH);
		item.addListener (SWT.Selection, new Listener () {
			public void handleEvent (Event e) {
				System.out.println ("Quit");
				shell.close();
			}
		});
		item.setText ("&Quit\tCtrl+Q");
		item.setAccelerator (SWT.MOD1 + 'Q');
		
		//Create Help menu
		MenuItem helpItem = new MenuItem (mainMenu, SWT.CASCADE);
		helpItem.setText ("&Help");
		Menu subHelpMenu = new Menu (shell, SWT.DROP_DOWN);
		helpItem.setMenu (subHelpMenu);
		MenuItem subHelpMenuitem = new MenuItem (subHelpMenu, SWT.PUSH);
		subHelpMenuitem.addListener (SWT.Selection, new Listener () {
			public void handleEvent (Event e) {
				System.out.println ("About");
			}
		});
		subHelpMenuitem.setText ("&About");
		
		//Define layout
		FormLayout formLayout = new FormLayout ();
		formLayout.marginWidth = 10;
		formLayout.marginHeight = 10;
		formLayout.spacing = 10;
		shell.setLayout (formLayout);
		
		//Create Download button
		Button downloadButton = new Button (shell, SWT.PUSH);
		downloadButton.setImage(img);
		downloadButton.setText ("Download &Images");
		FormData downloadButtonData = new FormData ();
		//downloadButtonData.top = new FormAttachment (label, 8);
		downloadButton.setLayoutData (downloadButtonData);
		
		//Create Settings button
		Button settingsButton = new Button (shell, SWT.PUSH);
		settingsButton.setText ("&Settings...");
		FormData settingsButtonData = new FormData ();
		settingsButtonData.left = new FormAttachment (downloadButton, 8);
		settingsButtonData.top = new FormAttachment (downloadButton, 0, SWT.TOP);
		settingsButton.setLayoutData (settingsButtonData);
		Image settingsImg = new Image(display,"settings-icon 48x48.png");
		settingsButton.setImage(settingsImg);
		
		
		//Create Status label
		Label statusLabel = new Label(shell, SWT.LEFT);
		statusLabel.setText("Copying nn of nn files...speed/s");
		FormData statusLabelData = new FormData ();
		statusLabelData.left = new FormAttachment (downloadButton, 0, SWT.LEFT);
		statusLabelData.top = new FormAttachment (downloadButton, 8);
		statusLabel.setLayoutData (statusLabelData);
		
		//Create Progressbar
		ProgressBar bar = new ProgressBar(shell, SWT.None);
		FormData barData = new FormData ();
		barData.left = new FormAttachment (statusLabel, 0, SWT.LEFT);
		barData.top = new FormAttachment (statusLabel, 8);
		bar.setLayoutData (barData);
		
		//Create Cancel button
		Button cancelButton = new Button (shell, SWT.PUSH);
		cancelButton.setText ("&Cancel");
		FormData cancelButtonData = new FormData ();
		cancelButtonData.left = new FormAttachment (bar, 8);
		cancelButtonData.top = new FormAttachment (bar, 0, SWT.TOP);
		cancelButton.setLayoutData (cancelButtonData);
		
		//Create Log
		Text log = new Text (shell, SWT.MULTI | SWT.READ_ONLY | SWT.BORDER);
		log.setText ("List of events\n");
		FormData logData = new FormData ();
		logData.left = new FormAttachment (bar, 0, SWT.LEFT);
		logData.top = new FormAttachment (bar, 8);
		log.setLayoutData (logData);
		
		
		//Center window on primary monitor
		shell.pack();
		Monitor primary = display.getPrimaryMonitor ();
		Rectangle bounds = primary.getBounds ();
		Rectangle rect = shell.getBounds ();
		int x = bounds.x + (bounds.width - rect.width) / 2;
		int y = bounds.y + (bounds.height - rect.height) / 2;
		shell.setLocation (x, y);
		
		shell.open ();
		return shell;
	}

}
